﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using hasApopo.algorithm;
using System.Text;

namespace hasApopo
{
     class Program
    {
        //RSAalgorithm RSA = new RSAalgorithm();
        //SHAalgorithm SHA = new SHAalgorithm();
        //Tools tools = new Tools();


        public static void initMyHash()
        {
            //init vars
            algorithm.SHAalgorithm.H = new UInt32[5];
            algorithm.SHAalgorithm.K = new UInt32[4];
            algorithm.SHAalgorithm.H[0] = 1732584193;
            algorithm.SHAalgorithm.H[1] = 4023233417;
            algorithm.SHAalgorithm.H[2] = 2562383102;
            algorithm.SHAalgorithm.H[3] = 271733878;
            algorithm.SHAalgorithm.H[4] = 3285377520;
            algorithm.SHAalgorithm.K[0] = 1518500249;
            algorithm.SHAalgorithm.K[1] = 1859775393;
            algorithm.SHAalgorithm.K[2] = 2400959708;
            algorithm.SHAalgorithm.K[3] = 3395469782;

            //create array of byte from ABCDE
            algorithm.SHAalgorithm.ABCDE = new byte[20]; // 20 byte = 20 * 8 = 160bit
            algorithm.SHAalgorithm.H1234 = new byte[16]; // 16byte = 16 * 8 = 128bit
            byte[] dump;

            //create array from element 0-3 from buffer and H
            for (int i = 0; i < 4; ++i)
            {
                dump = algorithm.SHAalgorithm.breakLongIntoArrByte(algorithm.SHAalgorithm.H[i], 4);
                byte[] dump2 = algorithm.SHAalgorithm.breakLongIntoArrByte(algorithm.SHAalgorithm.K[i], 4);
                for (int j = 0; j < 4; ++j)
                {
                    algorithm.SHAalgorithm.ABCDE[i * 4 + j] = dump[j];
                    algorithm.SHAalgorithm.H1234[i * 4 + j] = dump2[j];
                }
            }

            //create last 4 array from buffer
            dump = algorithm.SHAalgorithm.breakLongIntoArrByte(algorithm.SHAalgorithm.H[4], 4);
            for (int j = 0; j < 4; ++j)
            {
                algorithm.SHAalgorithm.ABCDE[16 + j] = dump[j];
            }
        }

        public static string ByteArrayToString(byte[] ba)
        {
            StringBuilder hex = new StringBuilder(ba.Length * 2);
            foreach (byte b in ba)
                hex.AppendFormat("{0:x2}", b);
            return hex.ToString();
        }

        // C# to convert a string to a byte array.
        public static byte[] StrToByteArray(string str)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            return encoding.GetBytes(str);
        }

        public byte[] SHAmessage(byte[] message)
        {
            byte[] mainresult = new byte[message.Length];
            initMyHash();
            byte[] tempmessage = algorithm.SHAalgorithm.insertPaddingAndMsgLength(message);
            return mainresult = algorithm.SHAalgorithm.getMessageDigest(tempmessage);
        }
       
        public String RSAEnkrip(byte[] message, String exp, String mod)
        {
            String hasilStr="";
            byte[] hasilRSA = new byte[message.Length * 2];

            hasilRSA=algorithm.RSAalgorithm.EnkripDekrip(message, exp, mod);
            hasilStr=ByteArrayToString(hasilRSA);

            return hasilStr;
        }

        public byte[] RSADekrip(String message, String exp, String mod)
        {
            byte[] hasildekrip = new byte [message.Length];
            byte[] temp = StrToByteArray(message);
            hasildekrip = algorithm.RSAalgorithm.EnkripDekrip(temp, exp, mod);

            return hasildekrip;
        }



        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new view.myHash());
        }
    }
}
